package uk.co.hephistocles.blobGame;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class Character {

	private int frames;
	public  int width;
	public int height;
	private int sheetWidth;

	private int tframes;
	private  int twidth;
	private int theight;
	private int tsheetWidth;
	private FileHandle tfileHandle;
	private float originX, originY;
	private float angle;
    private Sprite sprite;
    private int frameNum = 0;
    public boolean completed;
    
	public Character(int frames, int width, int height, int sheetWidth, FileHandle fileHandle) {
		this.frames = frames;
		this.width = width;
		this.height = height;
		this.sheetWidth = sheetWidth;
		Texture tex = new Texture(fileHandle);
		tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    	//prepare the spriteSheet of Player animation
        sprite = new Sprite(tex,width, height);
        completed = false;
	}
	public void setStats(int frames, int width, int height, int sheetWidth, FileHandle fileHandle) {
		this.frames = frames;
		this.width = width;
		this.height = height;
		this.sheetWidth = sheetWidth;
        completed = false;
        frameNum =0;

		Texture tex = new Texture(fileHandle);
		tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    	//prepare the spriteSheet of Player animation
        sprite = new Sprite(tex,width, height);
	}
	public Character(FileHandle fileHandle) {
		this.frames = 1;
		this.sheetWidth = 1;

		Texture tex = new Texture(fileHandle);
		tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    	//prepare the spriteSheet of Player animation
        sprite = new Sprite(tex);
		this.width = (int) sprite.getWidth();
		this.height = (int) sprite.getHeight();
        completed = false;
        frameNum =0;
	}
	
	public Character(Character sprt) {
		this.frames = sprt.frames;
		this.sheetWidth = sprt.sheetWidth;
        sprite = new Sprite(sprt.sprite);
		this.width = (int) sprite.getWidth();
		this.height = (int) sprite.getHeight();
        completed = false;
        frameNum =0;
	}
	
	public void randomFrame() {
		this.frameNum = (int)(Math.random()*frames);
	}
	
	public void setOrigin(float x, float y) {
		sprite.setOrigin(x,y);
		originX = x;
		originY = y;
	}
	
	public void center() {
		setOrigin(width/2,height/2);
	}
	
	public void setRotation(float a) {
		angle = a;
		sprite.setRotation(angle);
	}
	
	public void rotate(float a) {
		angle += a;
		sprite.setRotation(angle);
	}
	
	public void draw(int x, int y) {
		sprite.setRegion(frameNum%sheetWidth*width, height*(frameNum - frameNum%sheetWidth)/sheetWidth, width, height);

		sprite.setPosition(x-originX, y-originY);
		Drawer.sketch(sprite);
	}

	public float getX() {
		return sprite.getX();
	}
	public float getY() {
		return sprite.getY();
	}
	
	public void update(){
		//increase the animation's frame number
		frameNum++;
		//loop back to 0 at the end of animation
		if (frameNum>=frames) {
			frameNum = 0;
			completed = true;
		}
	}
	public float getRotation() {
		// TODO Auto-generated method stub
		return sprite.getRotation();
	}
}
